/*============================================================================
*  FILE: EpsModel.c
*=============================================================================
* Copyright  2025 Shanghai Hongqu Automobile Technology Co., LTD. 
* All rights reserved.
* Confidential - Restricted Shanghai Hongqu Automobile Technology Co., LTD. 
* Do not disclose.
*-----------------------------------------------------------------------------
*
*  DESCRIPTION:
*  	 Telltale EPS Str Logic
*
*
*===========================================================================*/

#include "EpsModel.h"
#include "audio_service_api.h"
#include "Common/DataStructureMacro.h"
#define LOG_TAG "TT"
#include "log_api.h"
#include "ssas_api.h"

//
#define EPS_SELFCHECK_COUNT 3

static void EpsGetEvent(EpsWorkingData* workData)
{
    uint16_t rawData;

    // TODO timeout
    // receive signal
    if (Com_ReceiveSignal(COM_SID_PwrStrIo, &rawData) != E_OK)
    {
        workData->event = EV_Unknown;
    }
    else
    {
        if (rawData == 0)
            workData->event = EV_Off;
        else if (rawData == 1)
            workData->event = EV_On;
        else
        {
            workData->event = EV_Unknown;

        }
    }
}

static void EpsStateTransition(EpsWorkingData* workData)
{
    uint16_t accState = getAccState();

    //acc off
    if (accState == ACC_OFF)
    {
        //TODO stop play sound
        workData->selfcheckCount = 0;
        workData->currentState = State_Init;
    }
    //acc on 
    else if (accState == ACC_ON)
    {
        //selfcheck when acc on
        switch (workData->currentState)
        {
            case State_Init:
            {
                if (accState == ACC_ON)
                {
                    workData->selfcheckCount = 0;
                    workData->currentState = State_Selfcheck;
                }
            }
            break;
            case State_Selfcheck:
            {
                workData->selfcheckCount++;

                if (workData->selfcheckCount >= EPS_SELFCHECK_COUNT)
                {
                    workData->selfcheckCount = 0;
                    if (workData->event == EV_On)
                        //TODO play sound
                        workData->currentState = State_On;
                    else
                        workData->currentState = State_Off;
                }
            }
            case State_Off:
            {
                if (workData->event == EV_On)
                    //TODO play sound
                    workData->currentState = State_On;
            }
            break;
            case State_On:
            {
                if (workData->event == EV_Off)
                    //TODO stop play sound
                    workData->currentState = State_Off;
            }
            break;
            default:
            break;
        }

    }
}

static void init()
{
    EpsWorkingDataInstance.currentState = State_Init;
    EpsWorkingDataInstance.event = EV_Unknown;
    EpsWorkingDataInstance.selfcheckCount = 0;
    EpsWorkingDataInstance.hasWidget = false;
}

static void release()
{

}

static void run()
{
    // get event 
    EpsGetEvent(&EpsWorkingDataInstance);
    // state transition
    EpsStateTransition(&EpsWorkingDataInstance);
}

EpsModel g_EpsModelInstance = {
    {
        init,
        run,
        release,
        MIN_PERIOD,
    },
};

// ----------------------------------View------------------------------
